<template>
  <div>
    <!-- 日期选择器 -->
    <div class="query-header">
      <span class="text">时间范围</span>
      <el-date-picker
        v-model="time"
        format="yyyy-MM-dd HH:mm:ss"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      >
      </el-date-picker>

      <el-button type="warning" class="query-btn" @click="queryData"
        >查询</el-button
      >
    </div>

    <column-cart title="订单统计" :options="options"></column-cart>
  </div>
</template>

<script>
import TimeRange from "@/components/timerange/TimeRange";

// 引入柱形图表组件
import ColumnCart from "@/components/echarts/ColumnCart.vue";

// 引入ajax工具函数
import { getOrderChart } from "@/api/echarts";

// 引入时间格式化的工具函数
import { normalizeDate } from "@/utils/tools";

export default {
  components: {
    TimeRange,
    ColumnCart,
  },
  data() {
    return {
      time: ["2020-10-01 00:00:00", "2023-10-10 00:00:00"],
      options: {
        xData: [],
        yData: [],
      },
    };
  },
  methods: {
    async getChartData() {
      let date = JSON.stringify(this.time);
      try {
        let res = await getOrderChart(date);
        let { data } = res.data;

        data.forEach(v => {
          this.options.xData.push(v.orderTime.substr(0,10))
          this.options.yData.push(v.orderAmount)
        })
      } catch (err) {
        console.log(err);
      }
    },
    queryData() {
     this.getChartData();
    },
  },

  created() {
    this.getChartData();
  },
};
</script>

<style lang="less" scoped>
.query-header {
  .text {
    margin-right: 10px;
  }

  /deep/.el-date-editor .el-range-separator {
    width: 20%;
  }

  .query-btn {
    margin-left: 10px;
  }

  /deep/.el-range-input {
    width: 200px;
  }
}
</style>